<%--
  Created by IntelliJ IDEA.
  User: Do
  Date: 2017/11/29
  Time: 17:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>WebSocket</title>
    <script src="http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js"></script>
    <script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.min.js"></script>
    <script>
      var url = 'http://'+window.location.host+'/trains';  //创建WebSockt
      var socket = new SockJS(url);
      var stomp = Stomp.over(socket);
      var payload = JSON.stringify({'message':'Hello'});


      //消息发送至"/app/async",订阅"/topic/async"
     /* stomp.connect('guest','guest',function(frame){
          console.log("*******Connected*******")
          stomp.subscribe("/topic/async",handleMessageFromServer);
          stomp.send("/app/async",{},payload);
      },function (error) {
          console.log(error);
      });

      function handleMessageFromServer(message) {
          console.log(message);
          console.log("Sending");
          stomp.send("/app/async",{},payload);
          console.log("Sent");
      }*/





    /*  //消息发送至"/app/async2",订阅"/topic/myDestination"(因为Server端使用@SendTo重定向代理目的地)
      stomp.connect('guest','guest',function(frame){
          console.log("*******Connected*******")
          stomp.subscribe("/topic/myDestination",handleMessageFromServer);
          stomp.send("/app/async2",{},payload);
      },function (error) {
          console.log(error);
      });

      function handleMessageFromServer(message) {
          console.log(message);
          console.log("Sending");
          stomp.send("/app/async2",{},payload);
          console.log("Sent");
      }
*/

      //消息发送至"/app/async3",订阅"/app/async3"(因为Server端使用@SubscribeMapping,不经过消息代理)
      stomp.connect('guest','guest',function(frame){
          console.log("*******Connected*******")
          stomp.subscribe("/app/async3",handleMessageFromServer);
          stomp.subscribe("/topic/async3",handleMessageFromServer);
      },function (error) {
          console.log(error);
      });

      function handleMessageFromServer(message) {
          console.log(message);
          console.log("Sending");
          console.log("Sent'");
      }



    </script>
  </head>
  <body>
    <button onclick="socket.close()">Close</button>
  </body>


</html>
